package com.gly091020.touhouLittleMad.mixin;

import com.github.tartaricacid.touhoulittlemaid.block.BlockJoy;
import com.github.tartaricacid.touhoulittlemaid.entity.item.EntitySit;
import com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid;
import com.gly091020.touhouLittleMad.event.MaidSittingInJoyEvent;
import net.minecraft.world.entity.Entity;
import net.neoforged.neoforge.common.NeoForge;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(EntitySit.class)
public abstract class JoySitMixin {
    @Inject(method = "tickMaid", at = @At("HEAD"))
    public void tickPre(EntityMaid maid, CallbackInfo ci){
        NeoForge.EVENT_BUS.post(new MaidSittingInJoyEvent.Tick.Pre(maid, (EntitySit)(Object)this));
    }

    @Inject(method = "tickMaid", at = @At("TAIL"))
    public void tickPost(EntityMaid maid, CallbackInfo ci){
        NeoForge.EVENT_BUS.post(new MaidSittingInJoyEvent.Tick.Post(maid, (EntitySit)(Object)this));
    }

    @Inject(method = "tickMaid", at = @At(value = "INVOKE", target = "Lcom/github/tartaricacid/touhoulittlemaid/entity/passive/EntityMaid;stopRiding()V"))
    public void stop(EntityMaid maid, CallbackInfo ci){
        NeoForge.EVENT_BUS.post(new MaidSittingInJoyEvent.Stop(maid, (EntitySit)(Object)this));
    }

    @Mixin(BlockJoy.class)
    public static class JoyBlockMixin{
        @Redirect(method = "startMaidSit", at = @At(value = "INVOKE", target = "Lcom/github/tartaricacid/touhoulittlemaid/entity/passive/EntityMaid;startRiding(Lnet/minecraft/world/entity/Entity;)Z"))
        public boolean start(EntityMaid maid, Entity entity){
            NeoForge.EVENT_BUS.post(new MaidSittingInJoyEvent.Start(maid, (EntitySit)entity));
            return maid.startRiding(entity);
        }
    }
}
